Jinja2 for loops
server.py
from flask import *app = Flask("iteration in jinja")ministers = ["ScoMo", "Turnbull", "Julia"]@app.route("/")def main():return render_template("template.html",names = ministers)app.run(debug=True)
templates\template.html
border="1"Prime Minister{% for n in names %}{{n}}{% endfor %}
you can also loop through a tuple, or a dictionary:
server.py
from flask import *app = Flask("more iteration in jinja")ministers = {"ScoMo":"Liberal","Turnbull":"Liberal","Julia":"Labor"}@app.route("/")def main():return render_template("template.html",names = ministers)app.run(debug=True)
templates\template.html
border="1"Prime MinisterParty{% for n in names|sort %}{{n}}{{names[n]}}{% endfor %}